package com.yuzhi.master.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yuzhi.common.core.page.PageDataInfo;
import com.yuzhi.common.util.StringUtils;

import com.yuzhi.master.system.domain.SysBasicInfo;
import com.yuzhi.master.system.domain.SysBasicInfoBo;
import com.yuzhi.master.system.mapper.SysBasicInfoMapper;
import com.yuzhi.master.system.request.SysBasicInfoReq;
import com.yuzhi.master.system.service.ISysBasicInfoService;
import com.yuzhi.master.system.vo.SysBasicInfoVo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 系统基础信息Service业务层处理
 *
 * @author auto code generator
 * @date 2024-04-12
 */
@RequiredArgsConstructor
@Service
public class SysBasicInfoServiceImpl implements ISysBasicInfoService {

    private final SysBasicInfoMapper baseMapper;

    /**
     * 查询系统基础信息
     */
    @Override
    public SysBasicInfoVo queryById(String orgCode){
        return baseMapper.selectVoById(orgCode);
    }

    /**
     * 查询系统基础信息列表
     */
    @Override
    public PageDataInfo<SysBasicInfoVo> queryPageList(SysBasicInfoReq request) {
        LambdaQueryWrapper<SysBasicInfo> lqw = buildQueryWrapper(request);
        Page<SysBasicInfoVo> result = baseMapper.selectVoPage(request.getPageQuery().build(), lqw);
        return PageDataInfo.build(result);
    }

    /**
     * 查询系统基础信息列表
     */
    @Override
    public List<SysBasicInfoVo> queryList(SysBasicInfoReq request) {
        LambdaQueryWrapper<SysBasicInfo> lqw = buildQueryWrapper(request);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<SysBasicInfo> buildQueryWrapper(SysBasicInfoReq request) {
        Map<String, Object> params = request.getParams();
        LambdaQueryWrapper<SysBasicInfo> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(request.getCopyright()), SysBasicInfo::getCopyright, request.getCopyright());
        lqw.eq(StringUtils.isNotBlank(request.getCurrentVersion()), SysBasicInfo::getCurrentVersion, request.getCurrentVersion());
        lqw.eq(StringUtils.isNotBlank(request.getHotline()), SysBasicInfo::getHotline, request.getHotline());
        lqw.eq(StringUtils.isNotBlank(request.getSupportPhone()), SysBasicInfo::getSupportPhone, request.getSupportPhone());
        return lqw;
    }

    /**
     * 新增系统基础信息
     */
    @Override
    public Boolean insertByBo(SysBasicInfoBo bo) {
        SysBasicInfo add = BeanUtil.toBean(bo, SysBasicInfo.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setOrgCode(add.getOrgCode());
        }
        return flag;
    }

    /**
     * 修改系统基础信息
     */
    @Override
    public Boolean updateByBo(SysBasicInfoBo bo) {
        SysBasicInfo update = BeanUtil.toBean(bo, SysBasicInfo.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(SysBasicInfo entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除系统基础信息
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
